<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
<div id="con"></div>
<script th:inline="javascript">
    let uid = [[${uid}]];
    console.log(uid)
    let chat = document.getElementById("con");
    if (window.EventSource) {
        //创建sse
        eventSource = new EventSource(`/createSse?uid=${uid}`);

        //监听sse
        eventSource.onopen = function (event) {
            console.log('SSE链接成功');
        }
        //监听sse
        eventSource.onmessage = function (event) {
            if(event.data){
                chat.innerHTML += event.data + '<br/>';
                console.log('后端返回的数据:', event.data);
            }
        }
        eventSource.onerror = (error) => {
            console.log('SSE链接失败');
        };

        eventSource.onclose = (close) => {
            console.log('SSE链接关闭');
        };
    } else {
        alert("你的浏览器不支持SSE");
    }
</script>
</body>
</html>